package com.cmj.homemodule;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cmj.commonmodule.CustPtrFramelayout;
import com.cmj.homemodule.adapter.HomeAdapter;
import com.cmj.homemodule.bean.HomeBean;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class HomeFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment_home, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        RecyclerView recyclyerView = view.findViewById(R.id.recyclyer);
        final CustPtrFramelayout custPtrFramelayout = view.findViewById(R.id.custPtrFramelayout);
        custPtrFramelayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return true;
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        custPtrFramelayout.refreshComplete();

                    }
                }, 2000);
            }
        });
        List<HomeBean> list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            HomeBean homeBean = new HomeBean();
            list.add(homeBean);
        }
        HomeAdapter homeAdapter = new HomeAdapter(R.layout.h_item_home, list);
        recyclyerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclyerView.setAdapter(homeAdapter);

    }
}
